package com.agileboot.attendance.controller;

import com.agileboot.attendance.domain.personnel.PersonnelApplicationService;
import com.agileboot.attendance.domain.personnel.command.AddPersonnelCommand;
import com.agileboot.attendance.domain.personnel.command.UpdatePersonnelCommand;
import com.agileboot.attendance.domain.personnel.dto.PersonnelDTO;
import com.agileboot.attendance.domain.personnel.dto.SearchPersonnelDTO;
import com.agileboot.attendance.domain.supplier.dto.SupplierOptionDTO;
import com.agileboot.common.core.base.BaseController;
import com.agileboot.common.core.dto.ResponseDTO;
import com.agileboot.common.core.page.PageDTO;
import com.agileboot.common.core.page.PageQuery;
import com.agileboot.common.core.command.BulkOperationCommand;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 * 人员管理 前端控制器
 * </p>
 *
 * @author agileboot
 * @since 2023-03-14
 */
@Validated
@RestController
@RequiredArgsConstructor
@Api(tags = "人员管理接口")
@RequestMapping("/attendance/personnel")
public class PersonnelController extends BaseController {

    private final PersonnelApplicationService personnelApplicationService;

    /**
     * 获取人员列表
     */
    @ApiOperation("获取人员列表")
//    @PreAuthorize("@permission.has('attendance:personnel:list')")
    @GetMapping("/list")
    public ResponseDTO<PageDTO<PersonnelDTO>> list(SearchPersonnelDTO searchPersonnelDTO, PageQuery pageQuery) {
        pageQuery.setParam(searchPersonnelDTO);
        PageDTO<PersonnelDTO> pageDTO = personnelApplicationService.getPersonnelList(pageQuery);
        return ResponseDTO.ok(pageDTO);
    }

    /**
     * 获取人员详情
     */
    @ApiOperation("获取人员详情")
    @PreAuthorize("@permission.has('attendance:personnel:query')")
    @GetMapping("/{personnelId}")
    public ResponseDTO<PersonnelDTO> getInfo(@PathVariable Long personnelId) {
        PersonnelDTO personnelDTO = personnelApplicationService.getPersonnelInfo(personnelId);
        return ResponseDTO.ok(personnelDTO);
    }

    /**
     * 新增人员
     */
    @ApiOperation("新增人员")
    @PreAuthorize("@permission.has('attendance:personnel:add')")
    @PostMapping
    public ResponseDTO<Void> add(@Valid @RequestBody AddPersonnelCommand addPersonnelCommand) {
        personnelApplicationService.addPersonnel(addPersonnelCommand);
        return ResponseDTO.ok();
    }

    /**
     * 修改人员
     */
    @ApiOperation("修改人员")
    @PreAuthorize("@permission.has('attendance:personnel:edit')")
    @PutMapping
    public ResponseDTO<Void> edit(@Valid @RequestBody UpdatePersonnelCommand updatePersonnelCommand) {
        personnelApplicationService.updatePersonnel(updatePersonnelCommand);
        return ResponseDTO.ok();
    }

    /**
     * 删除人员
     */
    @ApiOperation("删除人员")
    @PreAuthorize("@permission.has('attendance:personnel:remove')")
    @DeleteMapping
    public ResponseDTO<Void> remove(@Valid @RequestBody BulkOperationCommand<Long> deletePersonnelCommand) {
        personnelApplicationService.deletePersonnel(deletePersonnelCommand);
        return ResponseDTO.ok();
    }
    
    /**
     * 导出人员列表
     */
    @ApiOperation("导出人员列表")
    @PreAuthorize("@permission.has('attendance:personnel:export')")
    @GetMapping("/export")
    public void export(SearchPersonnelDTO searchPersonnelDTO, HttpServletResponse response) {
        personnelApplicationService.exportPersonnel(searchPersonnelDTO, response);
    }
    
    /**
     * 导入人员
     */
    @ApiOperation("导入人员")
    @PreAuthorize("@permission.has('attendance:personnel:import')")
    @PostMapping("/import")
    public ResponseDTO<Void> importPersonnel(MultipartFile file) {
        personnelApplicationService.importPersonnel(file);
        return ResponseDTO.ok();
    }
    
    /**
     * 下载导入模板
     */
    @ApiOperation("下载导入模板")
    @PreAuthorize("@permission.has('attendance:personnel:import')")
    @GetMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        personnelApplicationService.downloadImportTemplate(response);
    }
    
    /**
     * 获取供应商下拉列表
     */
    @ApiOperation("获取供应商下拉列表")
    @GetMapping("/suppliers")
    public ResponseDTO<List<SupplierOptionDTO>> getSupplierOptions() {
        List<SupplierOptionDTO> supplierOptions = personnelApplicationService.getSupplierOptions();
        return ResponseDTO.ok(supplierOptions);
    }
} 